﻿<?php

/**
 * User: yangcheng
 */
header ( "Content-type:text/html;charset=utf-8" );  //统一输出编码为utf-8  
$mysql_server_name = "qdm113416174.my3w.com"; //mysql数据库服务器
$mysql_username = "qdm113416174"; //mysql数据库用户名
$mysql_password = "yang147258369"; //mysql数据库密码
$mysql_database = "qdm113416174_db"; //mysql数据【库】名
$mysql_table = "carts"; //mysql数据【表】名


/**
 * 打印函数
 */
function println($msg)
{
    echo json_encode($msg);
}

/** 新建 连接 */
$mysqli = new mysqli($mysql_server_name, $mysql_username, $mysql_password);
/** 调试连接是否正常 */
//var_dump($mysqli);

/** 
 * check connection 
 */
if ($mysqli->connect_errno) {
	$arr['code'][] = 0;
	$arr['msg'][] = "Connect failed: ". $mysqli->connect_error;
    println($arr);
    exit();
}else{
	$mysqli->set_charset("utf8");//设置读、写数据的编码
	lists();
}


function lists(){

	global $mysqli,$mysql_database,$mysql_table;
	$mysqli->select_db($mysql_database);

	$token=$_SERVER['HTTP_TOKEN']  ;//获取token
	//判断是否登录
	if ($token != "13388107582") {
		$arr['code'][] = 403;
		$arr['msg'][] = "未登录，无权访问";
	    println($arr);
	    exit();
	}

	$page = $_GET['page']; //获取请求的页数 
	$limit = $_GET['limit']; //获取每页显示记录数 
	$sidx = $_GET['sidx']; //获取默认排序字段 
	$sord = $_GET['order']; //获取排序方式 
	$search = strtoupper(preg_replace('# #','',$_GET['search'])); //获取查询条件
	if (!$sidx) 
		 $sidx = 1; 
	 
	//执行SQL 
	$result = $mysqli->query("SELECT COUNT(*) AS count FROM $mysql_table WHERE idcard like '%$search%'"); 
	$row = $result->fetch_array(); 
	$count = $row['count']; //获取总记录数 
	//根据记录数分页 
	if ($count > 0) { 
	 	$total_pages = ceil($count / $limit); 
	} else { 
		 $total_pages = 0; 
	} 
	if ($page > $total_pages) 
	 $page = $total_pages; 
	$start = $limit * $page - $limit; 
	if ($start < 0 ) 
		$start = 0; 
	//执行分页SQL 
	$SQL = "SELECT * FROM carts WHERE idcard like '%$search%'  ORDER BY $sidx $sord LIMIT $start , $limit"; 
	$result = $mysqli->query($SQL) or die("Couldn t execute query." . $mysqli->connect_error); 
		//每页记录数
		// private int pageSize;
	// $responce->totalCount = $count; //总记录数 
	// $responce->totalPage = $total_pages; //总页数 
	// $responce->currPage = $page; //当前页 
	

	$i = 0; 
	//循环读取数据 
	while ($row = $result->fetch_array()) { 
	 $list[$i] = array ( 
		 "pid"  => intval($row['pid']), 
		 "idcard"  => $row['idcard'], 
		 "sys_content"  => $row['sys_content'], 
		 "sys_create_time"  => $row['sys_create_time'], 
		 "last_modify_time"  => $row['last_modify_time']
	 ); 
	 $i++; 
	} 

	// 页面
	$page = array (
	    "totalCount"  => $count,
	    "totalPage" =>$total_pages,
	    "currPage"   => $page,
	    "list"=>$list
	);
	
	$responce->page = $page; //当前页 
	$result->close();
	println($responce); //输出JSON数据 

}

/**
 * 关闭连接
 */
$mysqli->close();

